package guru.mercurio.core.ws;

import electric.registry.Registry;
import electric.server.http.HTTP;
import guru.mercurio.data.DataManager;
import guru.merlin.core.AbstractEnvelopeProcessor;
//import guru.merlin.core.CantHandException_;
import guru.merlin.core.ConfigurationEPException;
import guru.merlin.core.EPException;
import guru.merlin.core.PeerEPException;
import guru.merlin.core.Envelope;

import java.io.FileInputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class MercurioEP extends AbstractEnvelopeProcessor {
    Logger logger = Logger.getLogger(MercurioEP.class);

    public static Properties properties;

    private DataManager dm;


    public void openConnection(String parametersFilePath) throws EPException {
        try {
            FileInputStream is = new FileInputStream(parametersFilePath);
            MercurioEP.properties = new Properties();
            properties.load(is);

            String driver = properties.getProperty("db.driver");
            String dbmsURL = properties.getProperty("db.dbmsURL");
            String username = properties.getProperty("db.username");
            String password = properties.getProperty("db.password");
            dm = new DataManager(driver, dbmsURL, username, password);
        } catch (Exception e) {
            throw new PeerEPException("openConnection@MercurioEP" + e);
        }

    }

    public void start() {
        try {

            //String path = System.getProperty("path");
            String url_mercurio_wsdl = MercurioEP.properties.getProperty("url_mercurio_wsdl", "http://mercurio.twindivision.com:8081/merlin/");
            
            HTTP.startup(url_mercurio_wsdl);
            //HTTP.startup(url_mercurio_wsdl + mercurio_wsdl);

            // System.setOut(realOut);

            Registry.publish("mercurio", new guru.mercurio.core.ws.Mercurio(this, dm, MercurioEP.properties));
            logger.info("Service is up and running at " + url_mercurio_wsdl);
        } catch (Exception e) {
            logger.error("Error: " + e, e);
            stop();
        }

    }

    public void stop() {
        logger.info("Shuting down...");
        HTTP.shutdown();
        logger.info("done!");
    }

    /*
     * (non-Javadoc)
     * 
     * @see guru.merlin.core.AbstractEnvelopeProcessor#handEnvelope(guru.merlin.core.Envelope)
     */
    public Object handEnvelope(Envelope envelope) throws EPException {        
        throw new ConfigurationEPException(envelope.toString());
    }

    /* (sin Javadoc)
     * @see guru.merlin.core.EnvelopeHandler#retrieveEnvelope(java.lang.Object)
     */
    public void retrieveEnvelope(Object EnvelopeReference) throws EPException {
    }

    /* (sin Javadoc)
     * @see guru.merlin.core.EnvelopeHandler#commitHandEnvelope(java.lang.Object)
     */
    public void commitHandEnvelope(Object EnvelopeReference) throws EPException {
    }

    /**
     * @return Devuelve properties.
     */
    public Properties getProperties() {
        return MercurioEP.properties;
    }
    /**
     * @param properties El properties a establecer.
     */
    public void setProperties(Properties properties) {
        MercurioEP.properties = properties;
    }
}
